import { Component, OnInit } from "@angular/core";
import { ActivatedRoute, Router } from "@angular/router";
import { NzMessageService } from "ng-zorro-antd/message";
import { NzTableQueryParams } from "ng-zorro-antd/table";
import { DeviceInspectionTaskService } from "./device-inspection-task.service";
import { DictionaryService } from "../../sys/dictionary/dictionary.service";

@Component({
  selector: "app-device-inspection-task",
  templateUrl: "./device-inspection-task.component.html",
  styles: []
})
export class DeviceInspectionTaskComponent implements OnInit {


  loading = false;

  // 设备组数据
  deviceGroups = [];

  // 分页参数
  pageData = { page: 0, size: 10, total: null };
  // 查询条件
  searchBeans = [{
    attributeName: "code",
    searchType: "LIKE",
    attributeValue: ""
  }, {
    attributeName: "status",
    searchType: "EQ",
    numberValue: null
  }, {
    attributeName: "deviceInspectionPlan.inspectionType",
    searchType: "EQ",
    numberValue: null
  }];

  // 检验类型字典数据
  inspectionType = {};
  inspectionTypeArr = [];


  constructor(private deviceInspectionTaskService: DeviceInspectionTaskService,
              private msgSrv: NzMessageService,
              private activateInfo: ActivatedRoute,
              private router: Router,
              private dictionaryService: DictionaryService
  ) {
  }

  ngOnInit(): void {
    // 加载列表数据
    this.loadListData();
    // 加载字典数据
    this.loadDict("device_inspection_type");
  }

  _onReuseInit() {
    this.loadListData();
  }

  // 加载字典数据
  loadDict(dictCode) {
    this.dictionaryService.getDictItem(dictCode).subscribe(res => {
      this.inspectionTypeArr = res.data;
      res.data.forEach(item => {
        this.inspectionType[item.value] = item.label;
      });

    });
  }

  delete(id?) {
    this.loading = true;
    this.deviceInspectionTaskService.delete(id).subscribe((res) => {
      this.loading = false;
      if (res.code === 1) {
        this.msgSrv.success("删除完成");
      } else {
        this.msgSrv.error(res.message);
      }
    });
    console.log(id);


  }

  onSmt() {
    this.loadListData();
  }

  cancel() {
    this.msgSrv.success("已取消");
  }

  openEdit(record ?: any): void {

    if (record.status === 2) {
      this.router.navigateByUrl(`/device/device-inspection-handle/${record.id}/edit`);
    } else {
      this.msgSrv.info("此记录目前状态不可办理");
    }
  }

  openView(record) {
    this.router.navigateByUrl(`/device/device-inspection-handle/${record.id}/view`);
  }


  /**
   * 清除查询参数
   */
  clearParam() {

    this.searchBeans.forEach((item) => {
      item.attributeValue = "";
    });
  }

  /**
   * 加载列表数据
   */
  loadListData() {

    this.loading = true;

    const searchData = {
      page: this.pageData.page,
      size: this.pageData.size,
      searchFilter: {
        searchBeans: this.searchBeans
      }
    };

    this.deviceInspectionTaskService.findPage(searchData).subscribe((res) => {

      this.loading = false;

      if (res.code === 1) {
        this.pageData.page = res.data.number;
        this.pageData.size = res.data.size;
        this.pageData.total = res.data.totalElements;
        this.deviceGroups = res.data.content;
      } else {
        this.msgSrv.error(res.message);
      }

    });
  }

  onQueryParamsChange(params: NzTableQueryParams): void {

    const { pageSize, pageIndex, sort, filter } = params;

    this.pageData.page = pageIndex - 1;
    this.pageData.size = pageSize;

    this.loadListData();
  }

  // 导出报告
  export(task) {

    this.deviceInspectionTaskService.export(task.id).subscribe(res => {
      const blob = new Blob([res], { type: "text/plain;charset=utf-8" });
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.href = url;
      a.download = "检验报告" + task.code + ".docx";
      a.click();
      window.URL.revokeObjectURL(url);
    });
  }
}
